package com.example.demo.control;

import com.example.demo.pojo.UserCollection;
import com.example.demo.common.CommonResult;
import com.example.demo.mapper.UserErrorMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "错题接口")
@RestController
public class ErrorControl {

    @Autowired
    private UserErrorMapper userErrorMapper;

    @ApiOperation("获得错题列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="user_id",value="用户id",required=true,paramType="Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "正常返回，返回格式为\n" +
                    "id：错题记录id\n" +
                    "user_id:用户id\n" +
                    "item_type:题目类型\n" +
                    "item_id:题目id\n"),

    })
    @GetMapping("/selectError")
    public CommonResult selectCollection(@RequestParam Integer user_id){

        try {
            List<UserCollection> userCollections = userErrorMapper.getCollectionByUserId(user_id);
            return CommonResult.success(userCollections);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    @ApiOperation("添加错题")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(name="user_id",value="用户id",required=true,paramType="Integer"),
            @ApiImplicitParam(name="item_type",value="题目类型：1为单选题,2为多选题,3为判断题",required=true,paramType="Integer"),
            @ApiImplicitParam(name="item_id",value="该类型题目对应的id字段",required=true,paramType="Integer"),
    })
    @PostMapping("/addError")
    public CommonResult addCollection(@RequestParam Integer item_type,@RequestParam Integer item_id,@RequestParam Integer user_id){

        try {

            userErrorMapper.add(new UserCollection(user_id,item_type,item_id));
            return CommonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除错题")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="该错题记录的id字段",required=true,paramType="Integer"),
    })
    @PostMapping("/deleteError")
    public CommonResult deleteCollection(@RequestParam Integer id){

        try {
            userErrorMapper.deleteById(id);
            return CommonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }
}
